/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ads.presentation;

import adsrobotstub.RobotStub;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.logging.Logger;
import javax.swing.JTextArea;

/**
 * This class implements creating floor map by inputting coordinates of different
 * offices. The administrator needs to input all these information to get a floor
 * map each time the server is started or new user registers into the system.
 *
 * @author MFA
 */
public class AdminCreateFloorMapView extends javax.swing.JFrame {
    private static Logger logger = Logger.getLogger(AdminCreateFloorMapView.class.getName());
    private final ClientControllerInterface controller;
    private ArrayList<String[]> drawingItems;
    /**
     * Creates new form AdminCreateFloorMapView
     */
    public AdminCreateFloorMapView()
    {
        initComponents();
        controller=null;
    }
    /**
     *
     * @param c
     */
    public AdminCreateFloorMapView(ClientControllerInterface c) {
        drawingItems=new ArrayList();
        
        initComponents();
        controller = c;
        
        floorMapPanel.setBackground(Color.WHITE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        floorMapPanel = new javax.swing.JPanel(){
            @Override
            public void paintComponent(Graphics g)   {
                // Paint background
                super.paintComponent(g);

                // Get the drawing area
                int xt=10;
                int yt=10;
                int x=10;
                int y=10;
                int ovalL=10;
                // Set current drawing color
                g.setColor (Color.BLACK);
                String[] ts;
                // Draw a circle around the mid-point
                for(int i=0;i<drawingItems.size();i++)
                {
                    ts=drawingItems.get(i);
                    //unitSign for adapting drawing to the negative dists
                    //g.fillOval(x, y, 2, 2);
                    if(ts[0].equals("end"))
                    {
                        g.drawOval(x-(ovalL/2), y-(ovalL/2), ovalL, ovalL);
                        g.fillOval(x-(ovalL/2), y-(ovalL/2), ovalL, ovalL);
                    }
                    else
                    {
                        int unitSign=Integer.parseInt(ts[2])/Math.abs(Integer.parseInt(ts[2]));
                        if(ts[0].equals("start"))
                        {
                            g.drawOval(x-(ovalL/2), y-(ovalL/2), ovalL, ovalL);
                            g.fillOval(x-(ovalL/4), y-(ovalL/4), ovalL/2, ovalL/2);
                        }
                        else
                        {
                            g.drawOval(x-(ovalL/2), y-(ovalL/2), ovalL, ovalL);
                        }
                        if(ts[1].equals("x"))
                        {
                            xt=x + 10*Integer.parseInt(ts[2]);
                            yt=y;
                            g.drawLine(x+unitSign*ovalL/2, y, xt-unitSign*ovalL/2, yt);
                        }
                        else if(ts[1].equals("y"))
                        {
                            xt=x;
                            yt=y + 10*Integer.parseInt(ts[2]);
                            g.drawLine(x, y+ovalL/2, xt, yt-ovalL/2);
                        }
                        x=xt;
                        y=yt;
                    }
                }
            } // paintComponent
        };
        createMap = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        enterMapTA = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        clearMap = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        outputText = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        backButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout floorMapPanelLayout = new javax.swing.GroupLayout(floorMapPanel);
        floorMapPanel.setLayout(floorMapPanelLayout);
        floorMapPanelLayout.setHorizontalGroup(
            floorMapPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 553, Short.MAX_VALUE)
        );
        floorMapPanelLayout.setVerticalGroup(
            floorMapPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 239, Short.MAX_VALUE)
        );

        createMap.setText("Create Map");
        createMap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createMapActionPerformed(evt);
            }
        });

        enterMapTA.setColumns(20);
        enterMapTA.setRows(5);
        jScrollPane1.setViewportView(enterMapTA);

        jTextArea2.setEditable(false);
        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jTextArea2.setText("Sample Map Creation Code:\nstart/node1,x,10\nnode1/start,x,10--node2,y,7\nnode2/node1,y,7--node3,y,5\nnode3/node2,y,5--end,x,10\nend/node3,x,10");
        jScrollPane3.setViewportView(jTextArea2);
        jTextArea2.setEditable(false);

        clearMap.setText("Clear Map");
        clearMap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearMapActionPerformed(evt);
            }
        });

        outputText.setEditable(false);
        outputText.setColumns(20);
        outputText.setRows(5);
        jScrollPane2.setViewportView(outputText);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Output:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Enter map creation text here:");

        backButton.setText("Back");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING))
                            .addComponent(jLabel2))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel1)
                                            .addComponent(createMap)
                                            .addComponent(clearMap, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(0, 190, Short.MAX_VALUE))
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(backButton)
                                .addGap(112, 112, 112))))
                    .addComponent(floorMapPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(9, 9, 9)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(createMap)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(clearMap)
                        .addGap(11, 11, 11)
                        .addComponent(backButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(floorMapPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(8, 8, 8))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * This methods handles with the situation that "create map" button is clicked
     * 
     * @param evt the event that "create map" button is clicked
     */
    private void createMapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createMapActionPerformed
        String text=enterMapTA.getText();
        if(text.length()==0)
        {
            outputText.append("First enter text for map creation !\n\n");
        }    
        else
        {
            try{
                //handles with the text entered by administrator
                controller.createFloorMap(text,this);
                outputText.append("Map is successfully created.\n\n");
                //At last ready for drawing the map
                drawAction();
            }
            catch(Exception e)
            {
                outputText.append("Some problems with the Map creation syntax, please revise it !\n\n");
                e.printStackTrace();
            }
        
        }
    }//GEN-LAST:event_createMapActionPerformed

    /**
     * This method handles with the case that "Clear Map" button is clicked
     * 
     * @param evt 
     */
    private void clearMapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearMapActionPerformed
        //We need to clear the database of the offices
        try{
            controller.clearOffices(this);
            drawingItems.clear();
            floorMapPanel.repaint();
            outputText.append("Map is cleared.\n\n");
        }catch(Exception e)
        {
            outputText.append("Clear map operation could not be completed successfully.\n\n");
        }
    }//GEN-LAST:event_clearMapActionPerformed

    /**
     * This method handles the case where "Back" button is clicked.
     * <p>
     * the createfloormap window will close and return to the AdminMainView window
     * 
     * @param evt 
     */
    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        // set frame invisible
        this.setVisible(false);
        // free frame resources
        this.dispose();
        controller.stateAdminMain();
    }//GEN-LAST:event_backButtonActionPerformed
    
    /**
     * Draw the floormap according to map data generated formerly
     */
    public void drawAction()
    {
        drawingItems=controller.getMapDrawingArray();
        for(int i=0;i<drawingItems.size();i++)
        {
            String[] ts=drawingItems.get(i);
            logger.finest(ts[0]+" "+ts[1]+" "+ts[2]+" "+ts[3]+" "+ts[4]);
        }
        //Redraw the panel
        floorMapPanel.repaint();
    }
    
    /**
     * Output text to the window
     * 
     * @return 
     */
    public JTextArea getOutputText() {
        return outputText;
    }
    
    //Temporary main
    /**
     *
     * @param arasdas
     */
    public static void main(String[] arasdas)
    {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AdminCreateFloorMapView().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton;
    private javax.swing.JButton clearMap;
    private javax.swing.JButton createMap;
    private javax.swing.JTextArea enterMapTA;
    private javax.swing.JPanel floorMapPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextArea outputText;
    // End of variables declaration//GEN-END:variables

}
